<?php
class Mwc_SystemPoint_Model_Observer {
    public function createPoint($observer) {
        //Fetches the current event
        $event = $observer->getEvent();
        $setting = Mage::getModel('systempoint/setting')->load(1);

        $email = explode('@', $event->getCustomer()->getEmail());
        $email = $email[1];
        $collection = Mage::getModel('systempoint/group')
            ->getCollection()
            ->addFieldToFilter('email_format', array('eq' => "@$email"));
        $shadowPoint = $collection->getFirstItem()->getPoint();

        $log = Mage::getModel('systempoint/log');
        $log->setUserEmail($event->getCustomer()->getEmail());
        $log->setTitle($setting->getRegisterText());
        $log->setPoint($setting->getRegisterPoint());
        $log->setLogDate(date('Y-m-d H:i:s', mktime()));
        $log->save();

        $registerPoint = $setting->getRegisterPoint();
        $initialPoint = $shadowPoint > $registerPoint ? $shadowPoint : $registerPoint;
        if($shadowPoint > $registerPoint){
            $log = Mage::getModel('systempoint/log');
            $log->setUserEmail($event->getCustomer()->getEmail());
            $log->setTitle($setting->getInitialText());
            $log->setPoint($setting->getRegisterPoint());
            $log->setLogDate(date('Y-m-d H:i:s', mktime()));
            $log->save();
        }

        $userpoint = Mage::getModel('systempoint/userpoint');
        $userpoint->setEmail($event->getCustomer()->getEmail());
        $userpoint->setGroupTitle($setting->getDefaultGroup());
        $userpoint->setUserPoint($initialPoint);
        $userpoint->setAddDate(date('Y-m-d H:i:s', mktime()));
        $userpoint->setShadowPoint($shadowPoint);
        $userpoint->save();

        /*
        $mem = Mage::getModel('systempoint/membership')->loadHighestPoint();
        if($mem->getNumberOfPoint() < $setting->getRegisterPoint()){
            $vipl = Mage::getModel('systempoint/viplist');
            $vipl->setEmail($event->getCustomer()->getEmail());
            $vipl->setDate(date('Y-m-d H:i:s', mktime()));
            $vipl->save();
        }
        */
    }

    public function updatePoint($observer){
	    $event = $observer->getEvent();
	    $email = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
	    $log = Mage::getModel('systempoint/log');
	    $exp = Mage::getModel('systempoint/expire');
	    $userpoint = Mage::getModel('systempoint/userpoint');
	    
	    $oldestday = $log->loadOldestByEmail($email);
	    $days = $this->dateDiff($oldestday['log_date'],date('Y-m-d H:i:s', mktime()));
	    
	    $losepercent = $exp->getPercentByNoDay($days);
	    if($losepercent['percent_lose']!=''){
	    	//Find in log
	    	$check = $log->checkLog('Auto decrease '.$losepercent['percent_lose'].'% point', $email);
	    	if(!$check){
	    		$upoint = $userpoint->loadByEmail($email);	    	
	    		$tmppoint = $upoint['userpoint']*($losepercent['percent_lose']/100);	
	    		$pointafter = $upoint['userpoint'] - $tmppoint;
	    		
		        $log->setUserEmail($email);
		        $log->setTitle('Auto decrease '.$losepercent['percent_lose'].'% point');
		        $log->setPoint(-$pointafter);
		        $log->setLogDate(date('Y-m-d H:i:s', mktime()));
		        $log->save();
		        
                $predata['user_point'] = $pointafter;
        		$usrp = Mage::getModel('systempoint/userpoint')->load($upoint['itemid'])->addData($predata);
        		$usrp->setId($upoint['itemid'])->save();
	    	}
	    }
    }
    
    function dateDiff($start, $end) {
		$start_ts = strtotime($start);		
		$end_ts = strtotime($end);		
		$diff = $end_ts - $start_ts;		
		return round($diff / 86400);
	}
}